#include "my_led.h"



void my_led_ctrl(LED_TYPE_e led, LED_ON_OFF_e on_off_type)
{
    switch(led){
        case RED_LED:{
            switch(on_off_type){
                case LED_OFF: 
                    RED_LED_PORT->PBSC = RED_LED_PIN; 
                    break;
                case LED_ON:  
                    RED_LED_PORT->PBC = RED_LED_PIN;
                    break;
                case LED_BLINK:
                    RED_LED_PORT->POD ^= RED_LED_PIN;
                    break;
                default:
                    break;
            }
            break;
        }

        case GREEN_LED:{
            switch(on_off_type){
                case LED_OFF: 
                    GREEN_LED_PORT->PBSC = GREEN_LED_PIN; 
                    break;
                case LED_ON:  
                    GREEN_LED_PORT->PBC = GREEN_LED_PIN;
                    break;
                case LED_BLINK:
                    GREEN_LED_PORT->POD ^= GREEN_LED_PIN;
                    break;
                default:
                    break;
            }
            break;
        }
        case BLUE_LED:{
            switch(on_off_type){
                case LED_OFF: 
                    BLUE_LED_PORT->PBSC = BLUE_LED_PIN; 
                    break;
                case LED_ON:  
                    BLUE_LED_PORT->PBC = BLUE_LED_PIN;
                    break;
                case LED_BLINK:
                    BLUE_LED_PORT->POD ^= BLUE_LED_PIN;
                    break;
                default:
                    break;
            }
            break;
        }
        default:{
            break;
        }
    }
}


/**
 * @brief  Configures LED GPIO.
 * @param GPIOx x can be A to G to select the GPIO port.
 * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
 */
static void LedInit(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_InitType GPIO_InitStructure;

    /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

    /* Enable the GPIO Clock */
    if (GPIOx == GPIOA)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
    }
    else if (GPIOx == GPIOB)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
    }
    else if (GPIOx == GPIOC)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
    }
    else
    {
        if (GPIOx == GPIOD)
        {
            RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
        }
    }

    /* Configure the GPIO pin */
    if (Pin <= GPIO_PIN_ALL)
    {
        GPIO_InitStruct(&GPIO_InitStructure);
        GPIO_InitStructure.Pin        = Pin;
        GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
        GPIO_InitStructure.GPIO_Pull    = GPIO_No_Pull;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
        GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
    }
}

void my_led_init(void)
{
    LedInit(RED_LED_PORT, RED_LED_PIN);
    LedInit(BLUE_LED_PORT, BLUE_LED_PIN);
    LedInit(GREEN_LED_PORT, GREEN_LED_PIN);
}